package com.jy.modules.cims.component.common.service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.data.common.dao.TCMmContractDao;
import com.jy.modules.cims.data.common.dto.TCMmContractDTO;
import com.jy.platform.core.common.BaseDTO;

/**
 * @classname: TCMmContractService
 * @description: 定义 合约 实现类
 * @author: dell
 */
@Service("com.jy.modules.cims.component.common.service.TCMmContractService")
public class TCMmContractService implements Serializable {

	private static final long serialVersionUID = 1L;

	@Autowired
	private TCMmContractDao dao;

	/**
	 * @author dell
	 * @description: 分页查询 合约列表
	 * @date 2015-01-06 11:21:31
	 * @param searchParams
	 *            条件
	 * @return
	 * @throws
	 */
	public List<TCMmContractDTO> searchTCMmContractByPaging(Map<String, Object> searchParams) throws Exception {
		List<TCMmContractDTO> dataList = dao.searchTCMmContractByPaging(searchParams);
		return dataList;
	}

	/**
	 * @author dell
	 * @description: 按条件查询合约列表
	 * @date 2015-01-06 11:21:31
	 * @param searchParams
	 *            条件
	 * @return
	 * @throws
	 */
	public List<TCMmContractDTO> searchTCMmContract(Map<String, Object> searchParams) throws Exception {
		List<TCMmContractDTO> dataList = dao.searchTCMmContract(searchParams);
		return dataList;
	}

	/**
	 * @author dell
	 * @description: 查询合约对象
	 * @date 2015-01-06 11:21:31
	 * @param id
	 * @return
	 * @throws
	 */
	public TCMmContractDTO queryTCMmContractByPrimaryKey(String id) throws Exception {

		TCMmContractDTO dto = dao.findTCMmContractByPrimaryKey(id);

		if (dto == null)
			dto = new TCMmContractDTO();

		return dto;

	}

	/**
	 * @author shengzhoutao
	 * @description: 查询合约对象
	 * @date 2015-01-06 11:21:31
	 * @param contractCode
	 * @return
	 * @throws
	 */
	public TCMmContractDTO queryTCMmContractByContractCode(String contractCode) throws Exception {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		TCMmContractDTO contract = new TCMmContractDTO();
		contract.setContractCode(contractCode);
		searchParams.put("dto", contract);
		List<TCMmContractDTO> contractList = dao.searchTCMmContract(searchParams);

		contract = null;
		searchParams = null;
		if (CollectionsUtils.isEmpty(contractList)) {
			return null;
		}

		return contractList.get(0);
	}

	/**
	 * @title: insertTCMmContract
	 * @author shengzhoutao
	 * @description: 新增 合约对象
	 * @date 2015-01-06 11:21:31
	 * @param dto
	 * @return
	 * @throws
	 */
	@SuppressWarnings("all")
	public Long insertTCMmContract(TCMmContractDTO dto) throws Exception {
		TCMmContractDTO hisContract = queryTCMmContractByContractCode(dto.getContractCode());
		if (null != hisContract) {
			throw new Exception(
					java.text.MessageFormat.format("合约编号[{0}]已经存在！", new Object[] { dto.getContractCode() }));
		}

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);
		int count = dao.insertTCMmContract(paramMap);

		TCMmContractDTO resultDto = (TCMmContractDTO) paramMap.get("dto");
		Long keyId = resultDto.getId();
		return keyId;
	}

	/**
	 * @title: updateTCMmContract
	 * @author shengzhoutao
	 * @description: 修改 合约对象
	 * @date 2015-01-06 11:21:31
	 * @param paramMap
	 * @return
	 * @throws
	 */
	public void updateTCMmContract(TCMmContractDTO dto) throws Exception {
		TCMmContractDTO hisContract = dao.findTCMmContractByPrimaryKey(String.valueOf(dto.getId()));
		if (null == hisContract) {
			throw new Exception("原合约信息不存在！");
		}

		if (!hisContract.getContractCode().equals(dto.getContractCode())) {
			hisContract = queryTCMmContractByContractCode(dto.getContractCode());
			if (null != hisContract) {
				throw new Exception(java.text.MessageFormat.format("合约编号[{0}]已经存在！",
						new Object[] { dto.getContractCode() }));
			}
		}

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", dto);

		dao.updateTCMmContract(paramMap);
	}

	/**
	 * @title: deleteTCMmContractByPrimaryKey
	 * @author dell
	 * @description: 删除 合约,按主键
	 * @date 2015-01-06 11:21:31
	 * @param paramMap
	 * @throws
	 */
	public void deleteTCMmContractByPrimaryKey(BaseDTO baseDto, String ids) throws Exception {
		if (StringUtils.isEmpty(ids))
			throw new Exception("删除失败！传入的参数主键为null");

		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("dto", baseDto);
		paramMap.put("ids", ids);
		dao.deleteTCMmContractByPrimaryKey(paramMap);
	}

}
